// Create a datablock instance of Playerdata, and assign it the name DeadPlayer
// Copies datablock fields from PlayerBody
datablock PlayerData(DeadPlayer : PlayerBody)
{
	shapeFile = "~/data/shapes/player/player.dts";
	cmdCategory = "Dead";// Just changing this from 'Client' incase anyone ever needs to use it
	role = "Dead";
	name = "A corpse";
};


// We override Armor::onDisabled (DeadPlayer inherits this method from Armor by copying from PlayerBody)
// so that if this corpse is damaged, it won't be removed from the game.
// It's still subject to being pushed around by explosions though
function DeadPlayer::onDisabled(%this,%obj,%state)
{
}


// Tries to spawn the indexed numbered corpse in the SimGroup, if it exists
function DeadPlayer::spawn(%this, %index)
{
	%groupName = "MissionGroup/AIDropPoints/" @ %this.role;
    %group = nameToID(%groupName);
	if (%group != -1) 
	{
		%count = %group.getCount();
		if (%count != 0) 
		{
			%deadBody = new Player() {
				datablock = DeadPlayer;
			};

			// Add it to cleanup when mission ends
			MissionCleanup.add(%deadBody);

			// Get the location of the spawn sphere and spawn the bot there
			%spawnPoint = %group.getObject(%index);
			%deadBody.setTransform(%spawnPoint.getTransform());

			// Calling kill will tell the game to make the corpse non-coporeal, and keep it's health bar from displaying. 
			// Note: this doesn't cause the player to dissapear or play its death animation by itself
			%deadBody.kill();
			// There be red-shirts
			%deadBody.setSkinName("base_red");

			// Name to appear for corpse; fairly pointless since it shouldn't happen after calling kill(), but maybe useful in scripts
			%deadBody.setShapeName(%this.name);
			// Could make this random so they don't all look the same
			%deadBody.setActionThread("death4", true);
			return %deadBody;
		}
	}
	warn("!! DeadBody cannot be created: no spawn sphere for this index listed in " @ %groupName);
}


// Creates a corpse for each spawn point in the MissionGroup/AIDropPoints/Dead mission group
function CreateDead()
{
	// Create a corpse for every spawn point in this SimGroup.  Unfortunately we can't just pull this as a variable from DeadPlayer as the object won't exist the first tiem the script is run, thus throwing an error and not compiling
	if ( (%spawnGroup = nameToID("MissionGroup/AIDropPoints/Dead")) >= 0 )
	{
		%count = %spawnGroup.getCount();
		for ( %i = 0; %i < %count; %i++)
		{
			// Spawn the corpse
			DeadPlayer::spawn(DeadPlayer, %i);
		}
	}
}
